package com.laguille.games.tetris.applet;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import com.laguille.games.tetris.model.Model;
import com.laguille.games.tetris.model.Tetromino.Direction;

public class UserInputListener implements KeyListener {

	private Model model;

	public UserInputListener(Model model)
	{
		this.model = model;
	}
    
	/** Handle the key typed event from the text field. */
    public void keyTyped(KeyEvent e) {
        displayInfo(e, "KEY TYPED: ");
    }
    
    /** Handle the key pressed event from the text field. */
    public void keyPressed(KeyEvent e) {
        onKeyPressed(e);
    }
    
    /** Handle the key released event from the text field. */
    public void keyReleased(KeyEvent e) {
        onKeyReleased(e);
    }
    
    private void onKeyPressed(KeyEvent e)
    {
    	int id = e.getKeyCode();
    	switch (id)
    	{
    	case 37: //left
    		model.getField().movePiece(model.getCurrentPiece(), Direction.LEFT);
    		break;
    	case 38: //up
    		model.getField().rotatePiece(model.getCurrentPiece(), Direction.RIGHT);
    		break;
    	case 39: //right
    		model.getField().movePiece(model.getCurrentPiece(), Direction.RIGHT);
    		break;
    	case 40: //down
    		break;
    	default:
    		break;
    	}
    }

    private void onKeyReleased(KeyEvent e)
    {
    }

  
   
    /*
     * We have to jump through some hoops to avoid
     * trying to print non-printing characters
     * such as Shift.  (Not only do they not print,
     * but if you put them in a String, the characters
     * afterward won't show up in the text area.)
     */
    private void displayInfo(KeyEvent e, String keyStatus){
        
        //You should only rely on the key char if the event
        //is a key typed event.
        int id = e.getID();
        String keyString;
        if (id == KeyEvent.KEY_TYPED) {
            char c = e.getKeyChar();
            keyString = "key character = '" + c + "'";
        } else {
            int keyCode = e.getKeyCode();
            keyString = "key code = " + keyCode
                    + " ("
                    + KeyEvent.getKeyText(keyCode)
                    + ")";
        }
        
        int modifiersEx = e.getModifiersEx();
        String modString = "extended modifiers = " + modifiersEx;
        String tmpString = KeyEvent.getModifiersExText(modifiersEx);
        if (tmpString.length() > 0) {
            modString += " (" + tmpString + ")";
        } else {
            modString += " (no extended modifiers)";
        }
        
        String actionString = "action key? ";
        if (e.isActionKey()) {
            actionString += "YES";
        } else {
            actionString += "NO";
        }
        
        String locationString = "key location: ";
        int location = e.getKeyLocation();
        if (location == KeyEvent.KEY_LOCATION_STANDARD) {
            locationString += "standard";
        } else if (location == KeyEvent.KEY_LOCATION_LEFT) {
            locationString += "left";
        } else if (location == KeyEvent.KEY_LOCATION_RIGHT) {
            locationString += "right";
        } else if (location == KeyEvent.KEY_LOCATION_NUMPAD) {
            locationString += "numpad";
        } else { // (location == KeyEvent.KEY_LOCATION_UNKNOWN)
            locationString += "unknown";
        }
        
        String newline = "\n";
        System.out.println(keyStatus + newline
                + "    " + keyString + newline
                + "    " + modString + newline
                + "    " + actionString + newline
                + "    " + locationString + newline);
    }

}
